package com.ozo.easy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ozo.easy.common.model.*;
import com.ozo.easy.common.util.SortUtil;
import com.ozo.easy.common.exception.ServiceException;
import com.ozo.easy.exception.ServiceExceptionEnum;
import com.ozo.easy.pojo.vo.SysRoleDetailVO;
import com.ozo.easy.entity.SysRole;
import com.ozo.easy.mapper.SysRoleMapper;
import com.ozo.easy.pojo.dto.SysRoleSaveDTO;
import com.ozo.easy.pojo.dto.SysRoleQueryDTO;
import com.ozo.easy.pojo.vo.SysRoleVO;
import com.ozo.easy.service.ISysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 角色 服务实现类
 *
 * @author ozo
 * @version 1.0
 * @date 2025-02-14 16:53:43
 */
@RequiredArgsConstructor
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SysRoleSaveDTO saveDTO) {
        SysRole sysRole = BeanUtil.copyProperties(saveDTO, SysRole.class);
        sysRole.setEnableFlag(true);
        baseMapper.insert(sysRole);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(BaseBatchDTO<Long> batchDTO) {
        baseMapper.deleteBatchIds(batchDTO.getIdList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysRoleSaveDTO saveDTO) {
        SysRole sysRole = BeanUtil.copyProperties(saveDTO, SysRole.class);
        baseMapper.updateById(sysRole);
    }

    @Override
    public PageResult<SysRoleVO> page(BasePage basePage, SysRoleQueryDTO queryDTO) {
        Integer pageNum = basePage.getPageNum();
        Integer pageSize = basePage.getPageSize();

        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        // 排序
        String order = queryDTO.getOrder();
        if (StrUtil.isBlank(order)) {
            queryWrapper.lambda().orderByDesc(SysRole::getId);
        } else {
            SortUtil.setSortCondition(queryWrapper, queryDTO.getProp(), order);
        }
        // 条件
        queryWrapper.lambda()
                .like(StrUtil.isNotBlank(queryDTO.getRoleName()), SysRole::getRoleName, queryDTO.getRoleName())
                .like(StrUtil.isNotBlank(queryDTO.getRoleCode()), SysRole::getRoleCode, queryDTO.getRoleCode());
        Page<SysRole> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);

        PageResult<SysRoleVO> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setPage(page.getPages());
        pageResult.setTotal(page.getTotal());
        List<SysRole> recordList = page.getRecords();

        List<SysRoleVO> recordVOList = BeanUtil.copyToList(recordList, SysRoleVO.class);

        pageResult.setResults(recordVOList);
        return pageResult;
    }

    @Override
    public SysRoleDetailVO detail(Long id) {
        SysRole sysRole = baseMapper.selectById(id);
        if (Objects.isNull(sysRole)) {
            throw new ServiceException(ServiceExceptionEnum.DATA_NOT_EXISTS);
        }
        return BeanUtil.copyProperties(sysRole, SysRoleDetailVO.class);
    }

    @Override
    public void enable(Long id) {
        SysRole sysRole = baseMapper.selectById(id);
        if (Objects.isNull(sysRole)) {
            throw new ServiceException(ServiceExceptionEnum.DATA_NOT_EXISTS);
        }
        sysRole.setEnableFlag(!sysRole.getEnableFlag());
        baseMapper.updateById(sysRole);
    }

}
